package com.sgb.kjwl.view.ui.fragment.enterprise;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentProfessionalTeamBinding;
import com.sgb.kjwl.di.enterprise.DaggerEnterpriseFragComponent;
import com.sgb.kjwl.di.enterprise.EnterpriseFragModule;
import com.sgb.kjwl.model.entity.enterprise.EpTeamEntity;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.utils.DialogHelper;
import com.sgb.kjwl.view.ui.activity.enterprise.ProfessionalTeamDetailActivity;
import com.sgb.kjwl.view.ui.adapter.enterprise.EpTeamAdapter;
import com.sgb.kjwl.view.widget.DictWindow5;
import com.sgb.kjwl.view.widget.IpPopWindow;
import com.sgb.kjwl.view.widget.PickTwoLevelCityPopWindow;
import com.sgb.kjwl.view.widget.TwoAreaPopWindow;
import com.sgb.kjwl.viewmodel.enterprise.ProfessionalTeamModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

/**
 * 已弃用  现使用ProfessionalTeamFragment2
 */
@Deprecated
public class ProfessionalTeamFragment extends AppLazyFragment {

    FragmentProfessionalTeamBinding binding;

    @Inject
    ProfessionalTeamModel professionalTeamModel;

    private EpTeamAdapter epTeamAdapter;
    Map<String,Object> map = new HashMap<>();

    private IpPopWindow typePopWindow;//团队类别
    private List<IpPickSelectEntity> typeDatas;

    private IpPopWindow categoryPopWindow2;//行业分类二级分类
    List<IpPickSelectEntity> categoryDatas2 = new ArrayList<>();
    private PickTwoLevelCityPopWindow cityPopWindow2;
    List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> cityDatas2;

    String category;
    private boolean isChange3;
    private boolean isChange2;
    private boolean isChange1;


    public static ProfessionalTeamFragment newInstance(String param1, List<IpPickSelectEntity> list) {
        ProfessionalTeamFragment fragment = new ProfessionalTeamFragment();
        Bundle args = new Bundle();
        args.putString("category", param1);
        args.putSerializable("list", (Serializable) list);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerEnterpriseFragComponent.builder().appComponent(appComponent)
                .enterpriseFragModule(new EnterpriseFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_professional_team, container, false);
        return binding.getRoot();
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getArguments() != null) {
            category = getArguments().getString("category");
            categoryDatas2 = (List<IpPickSelectEntity>) getArguments().getSerializable("list");
            IpPickSelectEntity entity = new IpPickSelectEntity();
            entity.setValue("");
            entity.setName("不限");
            if(categoryDatas2 == null){
                categoryDatas2 = new ArrayList<>();
            }
            categoryDatas2.add(0, entity);

            categoryPopWindow2 = new IpPopWindow(getActivity());
            categoryPopWindow2.setFocusable(false);
            categoryPopWindow2.setOutsideTouchable(false);
            categoryPopWindow2.setOnResultClick((key1, key2, key3) -> {
                map.put("type", ((IpPickSelectEntity) key1).getValue());
                professionalTeamModel.getTeamQueryList(false, map);
                if (!((IpPickSelectEntity) key1).getName().equals("不限")) {
                    isChange1 = true;
                    setTextViewBg(binding.tvClass1, R.color.white, R.mipmap.icon_down_red, R.color.color_main);
                } else {
                    isChange1 = false;
                    setTextViewBg(binding.tvClass1, R.color.white, R.mipmap.ic_down_arrow_n, R.color.color_tab_two);
                    setDrwImage(binding.tvClass1, 2);
                }
            });
            categoryPopWindow2.setOnDismissListener(() -> {
                if (!isChange1) {
                    binding.tvClass1.setTextColor(getResources().getColor(R.color.color_666666));
                    setDrwImage(binding.tvClass1, 2);
                } else {
                    setDrwImage(binding.tvClass1, 1);
                }
            });
            categoryPopWindow2.setBgView(binding.grayLayout);
        }


    }

    @Override
    public void initView() {
        if (epTeamAdapter == null) {
            epTeamAdapter = new EpTeamAdapter(getActivity(), null);
        }
        binding.rl1.setOnClickListener(this);
        binding.rl2.setOnClickListener(this);
        binding.rl3.setOnClickListener(this);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.displayView.setLayoutManager(layoutManager);
        binding.displayView.setLoadingMoreEnabled(true);
        binding.displayView.setAdapter(epTeamAdapter);
        binding.displayView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                professionalTeamModel.getTeamQueryList(false, map);
            }

            @Override
            public void onLoadMore() {
                professionalTeamModel.getTeamQueryList(true, map);
            }
        });
        epTeamAdapter.OnCallPhone((num, name) -> {
            if (DialogHelper.checkLogin()) {
                DialogHelper.showDialog(getContext(), "确认呼叫", name + ":" + num, "确定", new DialogFunction() {
                    @Override
                    public void normalRoundDialog() {
                        ActivityJump.jumpToPhone(getContext(), num);
                    }
                });
            } else {
                DialogHelper.showLoginDialogNew(getContext(), 0,"温馨提示", "登录后可拨打电话,去登录", "登录");
            }
        });


        typePopWindow = new IpPopWindow(getActivity());
        typePopWindow.setOnResultClick((type, key2, key3) -> {
            map.put("publishType", ((IpPickSelectEntity) type).getValue());
            professionalTeamModel.getTeamQueryList(false, map);
            if (!((IpPickSelectEntity) type).getName().equals("不限")) {
                isChange2 = true;
                setTextViewBg(binding.tvClass2, R.color.white, R.mipmap.icon_down_red, R.color.color_main);
            } else {
                isChange2 = false;
                setTextViewBg(binding.tvClass2, R.color.white, R.mipmap.ic_down_arrow_n, R.color.color_tab_two);
                setDrwImage(binding.tvClass2, 2);
            }
        });
        typePopWindow.setOnDismissListener(() -> {
            if (!isChange2) {
                binding.tvClass2.setTextColor(getResources().getColor(R.color.color_666666));
                setDrwImage(binding.tvClass2, 2);
            } else {
                setDrwImage(binding.tvClass2, 1);
            }
        });
        typePopWindow.setBgView(binding.grayLayout);

        cityPopWindow2 = new PickTwoLevelCityPopWindow(getActivity());
        cityPopWindow2.setFocusable(false);
        cityPopWindow2.setOutsideTouchable(false);
        cityPopWindow2.setOnResultClick((key1, key2, key3) -> {
            map.put("provinceNo", key1);
            map.put("cityNo", key2);
            professionalTeamModel.getTeamQueryList(false, map);
            if (!key3.equals("不限")) {
                isChange3 = true;
                setTextViewBg(binding.tvClass3, R.color.white, R.mipmap.icon_down_red, R.color.color_main);
            } else {
                isChange3 = false;
                setTextViewBg(binding.tvClass3, R.color.white, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                setDrwImage(binding.tvClass3, 2);
            }

        });
        cityPopWindow2.setOnDisMissClick(() -> {
            if (!isChange3) {
                binding.tvClass3.setTextColor(getResources().getColor(R.color.color_666666));
                setDrwImage(binding.tvClass3, 2);
            } else {
                setDrwImage(binding.tvClass3, 1);
            }
        });

    }

    @Override
    protected void onLazyLoad() {
        map.put("category", category);
        professionalTeamModel.getTeamQueryList(false, map);
    }


    public void showList(List<EpTeamEntity> list, boolean isLoadMore) {
        binding.displayView.refreshComplete();
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                binding.displayView.loadMoreComplete();
            } else {
                binding.displayView.setNoMore(true);
            }
            binding.displayView.setVisibility(View.VISIBLE);
        } else {
            binding.displayView.refreshComplete();
            if (null != list && list.size() > 0) {
                binding.displayView.setVisibility(View.VISIBLE);
                binding.errorView.setVisibility(View.GONE);
                binding.displayView.setLoadingMoreEnabled(true);
            } else {
                epTeamAdapter.setDatas(null);
                binding.displayView.setVisibility(View.GONE);
                binding.errorView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_1:
                dismissWindow();
                if (!isChange1) {
                    if (!categoryPopWindow2.isShowing()) {
                        binding.tvClass1.setTextColor(getResources().getColor(R.color.new_blue));
                        setDrwImage(binding.tvClass1, 0);
                    }
                } else {
                    if (!categoryPopWindow2.isShowing()) {
                        setDrwImage(binding.tvClass1, 0);
                    }
                }
                showCategoryPopWindow2();
                break;
            case R.id.rl_2:
                dismissWindow();
                if (!isChange2) {
                    if (!typePopWindow.isShowing()) {
                        binding.tvClass2.setTextColor(getResources().getColor(R.color.new_blue));
                        setDrwImage(binding.tvClass2, 0);
                    }
                } else {
                    if (!typePopWindow.isShowing()) {
                        setDrwImage(binding.tvClass2, 0);
                    }
                }
                if (null != typeDatas && typeDatas.size() > 0) {
                    typePopWindow.showPopwindow(binding.roomLayout, 0, 0);
                } else {
                    professionalTeamModel.getTeamType();
                }
                break;
            case R.id.rl_3:
                dismissWindow();
                if (!isChange3) {
                    if (!cityPopWindow2.isShowing()) {
                        binding.tvClass3.setTextColor(getResources().getColor(R.color.new_blue));
                        setDrwImage(binding.tvClass3, 0);
                    }
                } else {
                    if (!cityPopWindow2.isShowing()) {
                        setDrwImage(binding.tvClass3, 0);
                    }
                }
                if (null != cityDatas2 && cityDatas2.size() > 0) {
                    cityPopWindow2.showPopwindow(binding.roomLayout, 0, 0);
                } else {
                    professionalTeamModel.getServiceCity();
                }
                break;
        }
    }

    public void showCategoryPopWindow2() {
        categoryPopWindow2.showPopwindow(binding.roomLayout, 0, 0);
        categoryPopWindow2.setData(categoryDatas2);
    }

    public void showTypePopWindow(List<IpPickSelectEntity> typeDatas) {
        this.typeDatas = typeDatas;
        typePopWindow.showPopwindow(binding.roomLayout, 0, 0);
        typePopWindow.setData(typeDatas);
    }

    public void showcityPopWindow2(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> cityDatas) {
        this.cityDatas2 = cityDatas;
        cityPopWindow2.showPopwindow(binding.roomLayout, 0, 0);
        cityPopWindow2.setData(cityDatas, false);
    }

    private void setDrwImage(TextView tv, int type) {
        Drawable img_off;
        Resources res = getResources();
        if (type == 0) {
            img_off = res.getDrawable(R.mipmap.icon_up_red);
        } else if (type == 1) {
            img_off = res.getDrawable(R.mipmap.icon_down_red);
        } else {
            img_off = res.getDrawable(R.mipmap.ic_down_arrow_n);
        }
        img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());
        tv.setCompoundDrawables(null, null, img_off, null); //设置右图标
    }

    private void setTextViewBg(TextView textView, int bg, int img, int textColor) {
        Drawable drawable_n = getResources().getDrawable(img);
        drawable_n.setBounds(0, 0, drawable_n.getMinimumWidth(), drawable_n.getMinimumHeight());
        textView.setCompoundDrawables(null, null, drawable_n, null);
        textView.setTextColor(getResources().getColor(textColor));
    }


    public void dismissWindow() {
        if (categoryPopWindow2.isShowing()) {
            categoryPopWindow2.dismiss();
        }
        if (typePopWindow.isShowing()) {
            typePopWindow.dismiss();
        }
        if (cityPopWindow2.isShowing()) {
            cityPopWindow2.dismiss();
        }

    }
}
